﻿using Newtonsoft.Json;

using RFIDLIB;

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using wareHouse;

namespace ShuoXiangProductionPlatform
{
    public partial class ShengChanFuMa : Form
    {
        public ShengChanFuMa()
        {
            InitializeComponent();

            this.FormBorderStyle = FormBorderStyle.None;
            this.button1.Hide();
            this.groupBox1.Hide();
            this.label12.Hide();

            this.dataGridView1.ReadOnly = true;
            this.dataGridView1.CellPainting += dataGridView1_CellPainting;
            this.dataGridView1.AutoGenerateColumns = false;
            dataGridView1.EnableHeadersVisualStyles = false;
            dataGridView1.ColumnHeadersDefaultCellStyle.Font = new Font("黑体", 10, FontStyle.Bold);
            dataGridView1.ColumnHeadersDefaultCellStyle.ForeColor = Color.Black;
            //列Header的背景色
            dataGridView1.ColumnHeadersDefaultCellStyle.BackColor = Color.Red;
            dataGridView1.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill;
            dataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing;
            dataGridView1.ColumnHeadersHeight = 30;
            dataGridView1.RowTemplate.Height = 30;

          
        }

       

        public void TeaRfidHelper_OngetTagDatas(string tagInfo)
        {
            
            if (isstart == true)
            {
                string[] arrs = tagInfo.Split(',');
                if (arrs.Length > 1 && arrs[0].Split(':').Length < 2)
                {
                    string uid = arrs[1];
                    var strurl = "http://rfid.jsxygkj.com/rfidreal/" + uid;
                    var databytes = Encoding.UTF8.GetBytes(strurl);
                    var PAYLOAD_LENGTH = Encoding.UTF8.GetBytes(strurl).Length + 1;
                    var sendbytes = new byte[3 + databytes.Length];
                    sendbytes[0] = (byte)PAYLOAD_LENGTH;
                    sendbytes[1] = 85;
                    sendbytes[2] = 04;
                    Array.Copy(databytes, 0, sendbytes, 3, databytes.Length);
                    TeaRfidHelper.byteurl = sendbytes;
                    CodeInfo codeInfo = new CodeInfo { orderNO = currentorderinfo.订单号, productId = currentorderinfo.产品ID, userId = Login.login.userInfo.logindata.userId, ndef = strurl, uid = uid };
                    string param = JsonConvert.SerializeObject(codeInfo);
                    HttpHelper httpHelper = new HttpHelper();
                    string res = httpHelper.HTTPJsonPost("http://47.102.195.242:28083/uid/submit/p", param);
                    var resjson = JsonConvert.DeserializeObject<JsonString.ApiMsgList<JsonString.orderlistdata>>(res);
                    if (TeaRfidHelper.Write() && resjson.success.Equals("T"))
                    {
                        this.label12.Text = "赋码成功!" + num;
                        this.label12.Show();
                        num++;
                        timer1.Enabled = true;
                        timer1.Interval = 1000;
                        timer1.Tick += Timer1_Tick;
                    }
                    else
                    {
                        this.label12.Text = "赋码失败" + resjson.msg;
                        this.label12.Show();
                        timer1.Enabled = true;
                        timer1.Interval = 1000;
                        timer1.Tick += Timer1_Tick;
                    }
                }

            }
        }



        /// <summary>
        /// 保存从接口获取到的订单信息
        /// </summary>
        public List<OrderInformation> orderInformations = new List<OrderInformation>();

        /// <summary>
        /// 每页记录数
        /// </summary>
        public int pageSize = 2;

        /// <summary>
        /// 总记录数
        /// </summary>
        public int recordCount = 0;

        /// <summary>
        /// 总页数
        /// </summary>
        public int pageCount = 0;

        /// <summary>
        /// 当前页
        /// </summary>
        public int currentPage = 1;

        /// <summary>
        /// 当前被选中的订单信息
        /// </summary>
        public OrderInformation currentorderinfo = new OrderInformation();


        private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
            if (e.ColumnIndex < 0 && e.RowIndex >= 0) // 绘制 自动序号
            {
                e.Paint(e.ClipBounds, DataGridViewPaintParts.All);
                Rectangle vRect = e.CellBounds;
                vRect.Inflate(-2, 2);
                TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(), e.CellStyle.Font, vRect, e.CellStyle.ForeColor, TextFormatFlags.Right | TextFormatFlags.VerticalCenter);
                e.Handled = true;
            }
            e.CellStyle.SelectionBackColor = Color.Gray; // 选中单元格时，背景色
            e.CellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter; //单位格内数据对齐方式

        }


        /// <summary>
        /// 是否开始赋码的标识
        /// </summary>
        public  bool isstart = false;


        /// <summary>
        /// 赋码按钮操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == dataGridView1.Columns["操作"].Index)
            {
                //MessageBox.Show(e.RowIndex.ToString());
                //获取当前被点击的单元格
                // DataGridViewButtonCell vCell = (DataGridViewButtonCell)dataGridView1.CurrentCell;
                dataGridView1.CurrentCell.Value = "继续";
                this.dataGridView1.Hide();
                this.groupBox2.Hide();
                this.button1.Show();
                this.groupBox1.Show();
                //显示订单详细信息
                currentorderinfo = orderInformations[((currentPage - 1) * pageSize + e.RowIndex)];
                this.label6.Text = currentorderinfo.赋码数量.ToString();
                this.label9.Text = currentorderinfo.已赋码数量.ToString();
                label1.Text = "订单号:" + currentorderinfo.订单号;
                label2.Text = "批次号:" + currentorderinfo.批次号;
                //暂时不需要
                label3.Text = "净含量:";
                label4.Text = "品名:" + currentorderinfo.产品名称;


            }

        }


        private void circleButton1_Click(object sender, EventArgs e)
        {
            try
            {
                if (isstart == false)
                {
                    this.circleButton1.Text = "暂停";
                    //开始赋码
                    LogHelper.WriteLog("用户" + Login.login.userInfo.logindata.name + "开始执行赋码");
                    isstart = true;



                }
                else
                {
                    this.circleButton1.Text = "开始";
                    //暂停赋码
                    isstart = false;
                }
            }
            catch (Exception ex)
            {

                LogHelper.WriteLog("写入读卡信息失败", ex);
            }

        }

        public static int num = 1;



        private void Timer1_Tick(object sender, EventArgs e)
        {
            this.label12.Hide();
            this.timer1.Enabled = false;

        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.groupBox1.Hide();
            this.dataGridView1.Show();
            this.button1.Hide();
        }

        /// <summary>
        /// 返回按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click_1(object sender, EventArgs e)
        {
            this.groupBox1.Hide();
            this.groupBox2.Show();
            this.dataGridView1.Show();
            this.button1.Hide();
            //停止赋码
        }

        /// <summary>
        /// 首页按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            if (currentPage == 1)
            {
                return;
            }
            currentPage = 1;
            LoadPage();
        }

        /// <summary>
        /// 尾页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button5_Click(object sender, EventArgs e)
        {
            if (currentPage == pageCount)
            { return; }
            currentPage = pageCount;
            LoadPage();
        }

        private void LoadPage()
        {

            recordCount = orderInformations.Count;
            pageCount = (recordCount / pageSize);
            if ((recordCount % pageSize) > 0)
            {
                pageCount++;
            }

            if (currentPage < 1) currentPage = 1;
            if (currentPage > pageCount) currentPage = pageCount;
            int beginRecord;    //开始指针
            int endRecord;      //结束指针
            beginRecord = pageSize * (currentPage - 1);
            if (currentPage == 1) beginRecord = 0;
            endRecord = pageSize * currentPage;

            if (currentPage == pageCount) endRecord = recordCount;

            dataGridView1.DataSource = null;
            List<OrderInformation> temp = new List<OrderInformation>();
            for (int i = beginRecord; i < endRecord; i++)
            {
                temp.Add(orderInformations[i]);
            }
            dataGridView1.DataSource = temp;
            label11.Text = "当前页: " + currentPage.ToString() + " / " + pageCount.ToString();//当前页
        }

        /// <summary>
        /// 上一页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            if (currentPage == 1)
            { return; }
            currentPage--;
            LoadPage();
        }

        /// <summary>
        /// 下一页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            if (currentPage == pageCount)
            { return; }
            currentPage++;
            LoadPage();
        }
    }
}
